﻿using FrameWork.Plugin;
using MyServer.Common;
using ScreenMonitor.Common;
using ScreenMonitor.Common.File;
using ScreenMonitor.Common.Objects;
using ScreenMonitor.ServerLib.Plugin;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace ScreenMonitor.ServerLib
{

    public static class TcpCommandOperater
    {
        #region========私有函数========

        private static object lock1 = new object();
        private static bool OperateSendBitMap(DataPackageCollection pkg)
        {
            lock (lock1)
            {
                foreach (ClientObject m in ClientManager.GetAllClient())
                {
                    if (m.Client == pkg.Tag)
                    {
                        m.ScreenImage = Util.BytesToImage(DataPacker.UnPackData(pkg));
                        m.State = ClientState.Connected;
                        return true;
                    }
                }
                return true;
            }
        }

        private static object lock2 = new object();
        private static bool OperateSendName(DataPackageCollection pkg)
        {
            lock (lock2)
            {
                foreach (ClientObject m in ClientManager.GetAllClient())
                {
                    if (m.Client == pkg.Tag)
                    {
                        m.InfoStr = Encoding.UTF8.GetString(DataPacker.UnPackData(pkg));
                        CommandObjects.TcpCommandCenter.SendCommand(CommandType.Start, (TcpClient)pkg.Tag);
                        return true;
                    }

                }
                return true;
            }
        }

        private static object lock3 = new object();
        private static bool OperateConnect(DataPackageCollection pkg)
        {
            lock (lock3)
            {
                CommandObjects.TcpCommandCenter.SendCommand(CommandType.GetName, (TcpClient)pkg.Tag);
                return true;
            }
        }

        private static object lock4 = new object();
        private static bool OperateSignCompelete(DataPackageCollection pkg)
        {
            lock (lock4)
            {
                SignResponceObject obj = SignResponceObject.FromByte(DataPacker.UnPackData(pkg));
                foreach (ClientObject m in ClientManager.GetAllClient())
                {
                    if (m.Client == pkg.Tag)
                    {
                        m.SignState = SignState.Signed;
                        m.StudentInfo = obj;
                    }
                }
                return true;
            }
        }

        private static object lock5 = new object();
        private static bool OperateSendFile(DataPackageCollection pkg)
        {
            lock (lock5)
            {
                FileRequestObject Obj = FileRequestObject.FromByte(DataPacker.UnPackData(pkg), pkg.Tag);
                FileRequestObject ReObj;
                FilePool.DoRequestObject(Obj, out ReObj);
                if (ReObj != null)
                {
                    CommandObjects.TcpCommandCenter.SendCommandWithData(CommandType.SendFile, ReObj.ToByte(), (TcpClient)pkg.Tag);
                }
                return true;
            }
        }

        private static object lock6 = new object();
        private static bool RunPluginCommand(string command, DataPackageCollection col)
        {
            lock (lock6)
            {
                foreach (IServerPlugin plu in PluginPool.GetPlugins())
                {
                    bool IsMatched = false;
                    foreach (string com in plu.GetCommands())
                    {
                        if (com == command)
                        {
                            plu.RunCommand(command, DataPacker.UnPackData(col));
                            IsMatched = true;
                            break;
                        }
                    }
                    if (IsMatched)
                        break;
                }
                return true;
            }
        }
        #endregion

        #region========公有函数========

        private static object runCommandLock = new object();
        public static bool RunCommand(DataPackageCollection pkg)
        {
            lock (runCommandLock)
            {
                try
                {
                    string command = Util.GetCommandLine(pkg.PackageName);
                    switch (command)
                    {
                        case CommandType.SendBitMap:
                            return OperateSendBitMap(pkg);
                        case CommandType.SendName:
                            return OperateSendName(pkg);
                        case CommandType.Connect:
                            return OperateConnect(pkg);
                        case CommandType.SignCompelete:
                            return OperateSignCompelete(pkg);
                        case CommandType.RePing:
                            return true;
                        case CommandType.SendFile:
                            return OperateSendFile(pkg);
                        default:
                            RunPluginCommand(command, pkg);
                            return false;
                    }
                }
                catch
                {
#if DEBUG
                throw;
#else
                    return false;
#endif
                }
            }
        }

        #endregion
    }
}
